package com.king.juc.syncmode;

/**
 * 交替输出线程演示
 * @author king
 * @date 2020/11/23 16:07
 */
public class WaitNofity {

    /**
     * 控制台打印
     * @param str 输出字符
     * @param flag 状态
     * @param nextFlag 下一状态
     */
    public void print(String str, int flag, int nextFlag){
        for (int i = 0; i < loopNumber; i++) {
            synchronized (this) {
                while(this.flag != flag){
                    try {
                        this.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.print(str);
                this.flag = nextFlag;
                this.notifyAll();
            }
        }
    }

    /**
     * 等待标记
     */
    private int flag;
    /**
     * 循环次数
     */
    private int loopNumber;

    public WaitNofity(int flag, int loopNumber) {
        this.flag = flag;
        this.loopNumber = loopNumber;
    }
}
